package org.diffshop.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.diffshop.common.utils.PageUtils;
import org.diffshop.common.utils.Query;
import org.diffshop.product.dao.CategoryBrandRelationDao;
import org.diffshop.product.dao.PmsBrandDao;
import org.diffshop.product.entity.PmsBrandEntity;
import org.diffshop.product.service.PmsBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service("pmsBrandService")
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandDao, PmsBrandEntity> implements PmsBrandService {
    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<PmsBrandEntity> queryWrapper = new LambdaQueryWrapper<PmsBrandEntity>();
        String key = (String) params.get("key");
        if (StringUtils.isNotBlank(key)){
            queryWrapper.eq(PmsBrandEntity::getBrandId, key).or().like(PmsBrandEntity::getName, key);
        }

        IPage<PmsBrandEntity> page = this.page(new Query<PmsBrandEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

    /**
     * 更新商品品牌
     * @param pmsBrand
     */
    @Transactional
    @Override
    public void updateDetail(PmsBrandEntity pmsBrand) {
        // 更新商品品牌
        this.updateById(pmsBrand);

        // 更新关联分类表中的信息
        categoryBrandRelationDao.updateBrand(pmsBrand.getBrandId(), pmsBrand.getName());

        // TODO: 2024/6/13 修改其他关联表
    }


}
